import { axios as request } from '@/utils/request'
import moment from 'moment'

// 分页查询
export function getPageList(params) {
  return request({
    url: `/workflow/model/all`,
    method: 'get',
    params
  })
}

// 查询模型
export function getModel(id) {
  return request({
    url: `/workflow/model/get/${id}`,
    method: 'get'
  })
}

// 查询模型的Png图片,图片以Base64形式返回
export function getModelPng(id) {
  return request({
    url: `/workflow/model/getModelPng/${id}`,
    method: 'get'
  })
}

// 查询模型的XML文件
export function getModelXml(id) {
  return request({
    url: `/workflow/model/getModelXml/${id}`,
    method: 'get'
  })
}

// 创建模型，返回模型ID
export function create(data) {
  return request({
    url: `/workflow/model/create`,
    method: 'post',
    data
  })
}

// 保存流程模型数据
export function save(data) {
  return request({
    url: `/workflow/model/save`,
    method: 'put',
    data
  })
}

// 删除流程模型
export function remove(ids) {
  return request({
    url: `/workflow/model/del/${ids}`,
    method: 'delete'
  })
}

export function defaultXml() {
  const rand = (Math.random() + '').substr(2, 4)
  const pattern = 'YYYYMMDDHHmmss'
  const dateStr = moment(new Date()).format(pattern)
  const sid = `sid_${dateStr}${rand}`
  const processId = `Process_${dateStr}${rand}`
  const xmlStr = `<?xml version="1.0" encoding="UTF-8"?>
  <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 
  xmlns:flowable="http://flowable.org/bpmn"
  xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" 
  xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" 
  xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  id="${sid}" 
  targetNamespace="http://bpmn.io/bpmn" 
  exporter="bpmn-js (https://demo.bpmn.io)" 
  exporterVersion="5.1.2">
  <process id="${processId}" isExecutable="true">
      <startEvent id="StartEvent_1y45yut" name="开始">
      <outgoing>SequenceFlow_0h21x7r</outgoing>
      </startEvent>
      <task id="Task_1hcentk">
      <incoming>SequenceFlow_0h21x7r</incoming>
      </task>
      <sequenceFlow id="SequenceFlow_0h21x7r" sourceRef="StartEvent_1y45yut" targetRef="Task_1hcentk" />
  </process>
  <bpmndi:BPMNDiagram id="BpmnDiagram_1">
      <bpmndi:BPMNPlane id="BpmnPlane_1" bpmnElement="${processId}">
      <bpmndi:BPMNShape id="StartEvent_1y45yut_di" bpmnElement="StartEvent_1y45yut">
          <omgdc:Bounds x="152" y="102" width="36" height="36" />
          <bpmndi:BPMNLabel>
          <omgdc:Bounds x="160" y="145" width="22" height="14" />
          </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Task_1hcentk_di" bpmnElement="Task_1hcentk">
          <omgdc:Bounds x="240" y="80" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0h21x7r_di" bpmnElement="SequenceFlow_0h21x7r">
          <omgdi:waypoint x="188" y="120" />
          <omgdi:waypoint x="240" y="120" />
      </bpmndi:BPMNEdge>
      </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
  </definitions>`
  return xmlStr
}
